package config

import (
	"gopkg.in/yaml.v2"
	"os"
)

type Nacos struct {
	Server    string `yaml:"server"`
	Port      uint64 `yaml:"port"`
	Path      string `default:"/nacos" yaml:"path"`
	Namespace string `yaml:"namespace"`
	Username  string `yaml:"username"`
	Password  string `yaml:"password"`
}

type Prometheus struct {
	Url        string `yaml:"url"`
	ConfigFile string `yaml:"configFile"`
	Group      string `default:"PrometheusConfig" yaml:"group"`
	DataId     string `default:"prometheus.yml" yaml:"dataId"`
}

type Rule struct {
	Url      string `yaml:"url"`
	RulePath string `yaml:"rulePath"`
	Group    string `default:"RuleConfig" yaml:"group"`
	DataId   string `default:"alert_rules" yaml:"dataId"`
}

type AlertManager struct {
	Url        string `yaml:"url"`
	Group      string `default:"AlertManagerConfig"  yaml:"group"`
	DataId     string `default:"alertmanager.yml" yaml:"dataId"`
	ConfigFile string `yaml:"configFile"`
}

type AppConfig struct {
	Nacos        *Nacos        `yaml:"nacos"`
	Prometheus   *Prometheus   `yaml:"prometheus"`
	AlertManager *AlertManager `yaml:"alertManager"`
	Rule         *Rule         `yaml:"rule"`
}

func (config *AppConfig) LoadConfig(filename string) error {

	//读取文件
	file, err := os.ReadFile(filename)
	if err != nil {
		return err
	}

	//解析yaml中的变量
	err = yaml.Unmarshal(file, config)
	if err != nil {
		return err
	}

	return nil
}
